#include "ctitlebar.h"
#include <QHBoxLayout>
#include <qt_windows.h>


CTitleBar::CTitleBar(QWidget* p)
    :QWidget(p)
{
    this->setAttribute(Qt::WA_DeleteOnClose);
    initUI();
}

CTitleBar::~CTitleBar()
{}

void CTitleBar::initUI()
{

    setAttribute(Qt::WA_StyledBackground);  //禁止父窗口影响子窗口
    this->setStyleSheet("background-color:rgb(54,54,54)");
    this->setFixedHeight(42);
    m_pLogo = new QLabel(this);
    m_pLogo->setFixedSize(32,32);
    m_pLogo->setStyleSheet("background-image:url(:/new/prefix1/resources/titlebar/title_icon.png);border:none");

    m_titleTextLabel = new QLabel(this);
    m_titleTextLabel->setText("我是标题");
    m_titleTextLabel->setFixedWidth(120);
m_titleTextLabel->setStyleSheet("QLabel{font-family: Microsoft YaHei; \
        font-size:18px; \
        color:#BDC8E2;background-color:rgb(54,54,54);}");


    m_pSetBtn = new QPushButton(this);
    m_pSetBtn->setFixedSize(32,32);
    m_pSetBtn->setStyleSheet("QPushButton{background-image:url(:/new/prefix1/resources/titlebar/set.svg);border:none}" \
                             "QPushButton:hover{" \
                             "background-color:rgb(99, 99, 99);" \
                             "background-image:url(:/new/prefix1/resources/titlebar/set_hover.svg);border:none;}");

    m_pMinBtn = new QPushButton(this);
    m_pMinBtn->setFixedSize(32,32);
    m_pMinBtn->setStyleSheet("QPushButton{background-image:url(:/new/prefix1/resources/titlebar/min.svg);border:none}" \
                             "QPushButton:hover{" \
                             "background-color:rgb(99, 99, 99);" \
                             "background-image:url(:/new/prefix1/resources/titlebar/min_hover.svg);border:none;}");

    m_PMaxBtn = new QPushButton(this);
    m_PMaxBtn->setFixedSize(32,32);
    m_PMaxBtn->setStyleSheet("QPushButton{background-image:url(:/new/prefix1/resources/titlebar/normal.svg);border:none}" \
                             "QPushButton:hover{" \
                             "background-color:rgb(99, 99, 99);" \
                             "background-image:url(:/new/prefix1/resources/titlebar/normal_hover.svg);border:none;}");

    m_pCloseBtn = new QPushButton(this);
    m_pCloseBtn->setFixedSize(32,32);
    m_pCloseBtn->setStyleSheet("QPushButton{background-image:url(:/new/prefix1/resources/titlebar/close.svg);border:none}" \
                               "QPushButton:hover{" \
                               "background-color:rgb(99, 99, 99);" \
                               "background-image:url(:/new/prefix1/resources/titlebar/close_hover.svg);border:none;}");

    QHBoxLayout* pHlay = new QHBoxLayout(this);
    pHlay->addWidget(m_pLogo);
    pHlay->addWidget(m_titleTextLabel);
    pHlay->addStretch();
    pHlay->addWidget(m_pSetBtn);
    QSpacerItem* pItem1 = new QSpacerItem(20,20,QSizePolicy::Fixed, QSizePolicy::Fixed);
    pHlay->addSpacerItem(pItem1);

    pHlay->addWidget(m_pMinBtn);
    QSpacerItem* pItem2 = new QSpacerItem(20,20,QSizePolicy::Fixed, QSizePolicy::Fixed);
    pHlay->addSpacerItem(pItem2);

    pHlay->addWidget(m_PMaxBtn);
    QSpacerItem* pItem3 = new QSpacerItem(20,20,QSizePolicy::Fixed, QSizePolicy::Fixed);
    pHlay->addSpacerItem(pItem3);

    pHlay->addWidget(m_pCloseBtn);

    pHlay->setContentsMargins(5,5,5,5);
    connect(m_pMinBtn, &QPushButton::clicked, this, &CTitleBar::onClicked);
    connect(m_PMaxBtn, &QPushButton::clicked, this, &CTitleBar::onClicked);
    connect(m_pCloseBtn, &QPushButton::clicked, this, &CTitleBar::onClicked);
}

void CTitleBar::mousePressEvent(QMouseEvent *event)
{

    if (ReleaseCapture())
    {
        QWidget* pWindow = this->window();
        if (pWindow->isWindow())
        {
            SendMessage(HWND(pWindow->winId()), WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }
    }
}

void CTitleBar::onClicked()
{
    QPushButton* pButton = qobject_cast<QPushButton*>(sender());    //获取到是哪个按钮点击的
    QWidget* pWindow = this->window();      //拿到父窗口指针

    if(pButton == m_pMinBtn)
    {
        pWindow->showMinimized();
    }
    else if(pButton == m_PMaxBtn)
    {
        if(pWindow->isMaximized())
        {
            pWindow->showNormal();
            m_PMaxBtn->setStyleSheet("QPushButton{background-image:url(:/new/prefix1/resources/titlebar/normal.svg);border:none}" \
                                     "QPushButton:hover{" \
                                     "background-color:rgb(99, 99, 99);" \
                                     "background-image:url(:/new/prefix1/resources/titlebar/normal_hover.svg);border:none;}");
        }
        else
        {
            pWindow->showMaximized();
            m_PMaxBtn->setStyleSheet("QPushButton{background-image:url(:/new/prefix1/resources/titlebar/max.svg);border:none}" \
                                     "QPushButton:hover{" \
                                     "background-color:rgb(99, 99, 99);" \
                                     "background-image:url(:/new/prefix1/resources/titlebar/max_hover.svg);border:none;}");
        }
    }
    else if(pButton == m_pCloseBtn)
    {
        emit sig_close();
    }
}

void CTitleBar::mouseDoubleClickEvent(QMouseEvent *event)
{
    //默认的槽函数,也可以用信号clicked（）
    m_PMaxBtn->click();
 }


